/*
 Ajax 三级省市联动
 see: http://www.helloweba.com/view-blog-188.html

 settings 参数说明
 -----
 url:省市数据josn文件路径
 prov:默认省份
 city:默认城市
 dist:默认地区（县）
 nodata:无数据状态
 required:必选项
 ------------------------------ */
(function ($) {
    $.fn.citySelect = function (settings) {
        if (this.length < 1) {
            return;
        }
        
        // 默认值
        settings = $.extend({
            url: "js/city.min.js",
            prov: null,
            city: null,
            dist: null,
            nodata: null,
            required: true
        }, settings);
        var boxObj = this;
        var provObj = boxObj.find(".prov");
        var cityObj = boxObj.find(".city");
        var distObj = boxObj.find(".dist");
        var provVal = settings.prov;
        var cityVal = settings.city;
        var distVal = settings.dist;
        var selectPrehtml = (settings.required) ? "" : "<option value=''>请选择</option>";
        var cityJson;
        var tempHtml;
        // 去除 disabled 属性
        if (cityObj.attr("disabled")) {
            cityObj.attr("disabled", false);
        }
        if (distObj.attr("disabled")) {
            distObj.attr("disabled", false);
        }
        // 赋值市级函数
        var cityStart = function () {
            var provId = provObj.get(0).selectedIndex;
            if (!settings.required) {
                provId--;
            }
            cityObj.empty().attr("readonly", true);
            distObj.empty().attr("readonly", true);
            if (provId < 0 || typeof(cityJson.citylist[provId].c) === "undefined") {
                if (settings.nodata === "none") {
                    cityObj.css("display", "none");
                    distObj.css("display", "none");
                } else if (settings.nodata === "hidden") {
                    cityObj.css("visibility", "hidden");
                    distObj.css("visibility", "hidden");
                }
                return;
            }
            // 遍历赋值市级下拉列表
            tempHtml = selectPrehtml;
            $.each(cityJson.citylist[provId].c, function (i, city) {
                tempHtml += "<option value='" + city.id + "'>" + city.n + "</option>";
            });
            cityObj.html(tempHtml).attr("readonly", false).css({"display": "", "visibility": ""});
            distStart();
        };
        // 赋值地区（县）函数
        var distStart = function () {
            var provId = provObj.get(0).selectedIndex;
            var cityId = cityObj.get(0).selectedIndex;
            if (!settings.required) {
                provId--;
                cityId--;
            }
            distObj.empty().attr("readonly", true);
            if (provId < 0 || cityId < 0 || typeof(cityJson.citylist[provId].c[cityId].a) === "undefined") {
                if (settings.nodata === "none") {
                    distObj.css("display", "none");
                } else if (settings.nodata === "hidden") {
                    distObj.css("visibility", "hidden");
                }
                return;
            }
            
            // 遍历赋值市级下拉列表
            tempHtml = selectPrehtml;
            $.each(cityJson.citylist[provId].c[cityId].a, function (i, dist) {
                tempHtml += "<option value='" + dist.id + "'>" + dist.s + "</option>";
            });
            distObj.html(tempHtml).attr("readonly", false).css({"display": "", "visibility": ""});
        };
        var init = function () {
            // 遍历赋值省份下拉列表
            tempHtml = selectPrehtml;
            $.each(cityJson.citylist, function (i, prov) {
                tempHtml += "<option value='" + prov.id + "'>" + prov.p + "</option>";
            });
            provObj.html(tempHtml);
            // 若有传入省份与市级的值，则选中。（setTimeout为兼容IE6而设置）
            setTimeout(function () {
                if (settings.prov != null) {
                    provObj.val(settings.prov);
                    cityStart();
                    setTimeout(function () {
                        if (settings.city != null) {
                            cityObj.val(settings.city);
                            distStart();
                            setTimeout(function () {
                                if (settings.dist != null) {
                                    distObj.val(settings.dist);
                                }
                            }, 1);
                        }
                    }, 1);
                }
            }, 1);
            // 选择省份时发生事件
            provObj.bind("change", function () {
                cityStart();
            });
            // 选择市级时发生事件
            cityObj.bind("change", function () {
                distStart();
            });
        };
        // 设置省市json数据
        if (typeof(settings.url) === "string") {
            $.getJSON(settings.url, function (json) {
                cityJson = json;
                init();
            });
        } else {
            cityJson = settings.url;
            init();
        }
    };
})(jQuery);